﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using Eloquera.Client;

namespace _430GroupProject
{
    /// <summary>
    /// Internal system message.
    /// </summary>
    [DataContractAttribute]
    public class Message : SuperClass
    {
        //ID used by database.
        [DataMember, ID]
        public long DBID;

        //Unique ID for message used by system.
        [DataMember]
        public Guid MessageID { get; private set; }

        //Header or subject or message.
        [DataMember]
        public String Header { get; set; }

        //Body or content of message.
        [DataMember]
        public String Body { get; set; }

        //userId of message creator
        [DataMember]
        public String From { get; set; }

        //userId's of people who recive the message
        [DataMember]
        public List<String> To { get; set; }

        //userIDs of recipients of the message.
        [DataMember]
        public String RecipientList { get; set; }

        //Who has read/ not read the message
        [DataMember]
        public List<bool> Read { get; set; }

        // boolean status of the message in string form.
        [DataMember]
        public String Status { get; set; }

        /// <summary>
        /// Creates a new message from the given user (from), to the given user (to).
        /// </summary>
        /// <param name="from">The user who is sending the message.</param>
        /// <param name="to">The user who is recieving the message.</param>
        /// <param name="body">The body of the message.</param>
        public Message(String from, String to, String body, String head) {
            this.MessageID = Guid.NewGuid();
            this.To = new List<String>();
            this.Header = head;
            To.Add(to);
            RecipientList = to;
            this.From = from;
            this.Body = body;
            initRead(1);
            
        }

        /// <summary>
        /// Creates a new message from the given user, to the given list of users.
        /// </summary>
        /// <param name="from">The user who is sending the message.</param>
        /// <param name="to">The users who are recieving the message.</param>
        /// <param name="body">The body of the message.</param>
        public Message(String from, List<String> to, String body, String head)
        {
            this.MessageID = Guid.NewGuid();
            this.Header = head;
            this.Body = body;
            this.From = from;
            this.To = to;
            RecipientList = "";
            Status = "";
            
            foreach(string r in to)
            {
                RecipientList += r + ", ";
            }

            RecipientList.TrimEnd(',');
            
            initRead(to.Count);
        }

        /// <summary>
        /// Initalizes the Read with the given size.
        /// </summary>
        /// <param name="size">The size of the read array, should be the same as the To array.</param>
        private void initRead(int size) {
            Status = "";
            this.Read = new List<bool>();
            for (int i = 0; i < size; i++) {
                Read.Add(false);
                Status += To[i] + ": Unread, ";
            }
            Status.TrimEnd(',');
        }

        /// <summary>
        /// Marks this message as read for the given user.
        /// </summary>
        /// <param name="userID">The user who read the message.</param>
        public void markAsRead(User user)
        {
            markAsRead(user.UserID);
        }

        /// <summary>
        /// Marks this message as read for the given user.
        /// </summary>
        /// <param name="userID">The user who read the message.</param>
        public void markAsRead(String userID) {
            for (int i = 0; i < To.Count; i++) {
                if (To[i].Equals(userID)) Read[i] = true;
            }
        }
    }
}
